home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 9825 / 9825.xpi / components / smarterwiki-http-observer.js next >
Text File  |  2009-08-10  |  9KB  |  230 lines

  1. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  2.  
  3. function LOG(msg)
  4. {
  5.     var enabled = false;
  6.     if(enabled)
  7.     {
  8.         var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
  9.             .getService(Components.interfaces.nsIConsoleService);
  10.         consoleService.logStringMessage("smarterwiki smarterwiki-http-observer.js: " + msg);
  11.     }
  12. }
  13.  
  14.  
  15. function SmarterWikiHTTPObserver() { }
  16.  
  17. var getDOMWindow = function(contentWindow)
  18. {
  19.     try
  20.     {
  21.         return contentWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  22.                             .getInterface(Components.interfaces.nsIWebNavigation)
  23.                             .QueryInterface(Components.interfaces.nsIDocShell)
  24.                             .QueryInterface(Components.interfaces.nsIDocShellTreeItem).rootTreeItem
  25.                             .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  26.                             .getInterface(Components.interfaces.nsIDOMWindow).top;
  27.     }
  28.     catch(e)
  29.     {
  30.         return null;
  31.     }
  32. };
  33.  
  34.  
  35. var content_type_to_ext = {
  36.     "video/x-flv": "flv", 
  37.     "application/x-shockwave-flash": "swf",
  38.     "image/jpeg": "jpg",
  39.     "image/png": "png",
  40.     "image/x-icon": "ico",
  41.     };
  42.  
  43. var media_types = {
  44.     "flv": null,
  45.     "swf": null,
  46. };
  47.  
  48.  
  49. SmarterWikiHTTPObserver.prototype = {
  50.     observe: function(aSubject, aTopic, aData)
  51.     {
  52.         try
  53.         {
  54.             if(aTopic == "http-on-examine-response" ||
  55.                 aTopic == "http-on-examine-cached-response")
  56.             {
  57.                 aSubject.QueryInterface(Components.interfaces.nsIHttpChannel);
  58.                 var httpChannel = aSubject;
  59.                 try
  60.                 {
  61.                     var contentType = httpChannel.getResponseHeader("Content-Type");
  62.                     if(contentType in content_type_to_ext)
  63.                     {
  64.                         LOG("Got: " + contentType);
  65.                         var uri = httpChannel.originalURI;
  66.                         try
  67.                         {
  68.                             var document = (httpChannel.notificationCallbacks || httpChannel.loadGroup.groupObserver)
  69.                                                             .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  70.                                                             .getInterface(Components.interfaces.nsIDOMWindow).document;
  71.  
  72.                             var doc = document;
  73.                             for(var i = 0; i < 10; i++)//prevents infinite loop, just in case
  74.                             {
  75.                                 if(!doc.SW_uri_content_type)
  76.                                 {
  77.                                     doc.SW_uri_content_type = {};
  78.                                 }
  79.                                 doc.SW_uri_content_type[uri.spec] = contentType;
  80.                                 if(!doc.defaultView || doc == doc.defaultView.top)
  81.                                 {
  82.                                     break;
  83.                                 }
  84.                                 else
  85.                                 {
  86.                                     doc = doc.defaultView.parent.document;
  87.                                 }
  88.                             }
  89.                             
  90.                             var file_ext = content_type_to_ext[contentType];
  91.                             
  92.                             if(file_ext in media_types)
  93.                             {
  94.                                 var referrer = httpChannel.referrer;
  95.                                 var filename = null;
  96.                                 try
  97.                                 {
  98.                                     //Example: Content-Disposition   'attachment; filename="video.flv"'
  99.                                     var contentDisposition = httpChannel.getResponseHeader("Content-Disposition");
  100.                                     filename = /attachment; filename="(.*)"/.exec(contentDisposition)[1]
  101.                                 }
  102.                                 catch(e) {}
  103.  
  104.                                 try
  105.                                 {
  106.                                     var contentLength = parseInt(httpChannel.getResponseHeader("Content-Length"));
  107.                                     if(contentLength < 100 * 1024)
  108.                                     {
  109.                                         return;
  110.                                     }
  111.                                 }
  112.                                 catch(e) {}
  113.                                 
  114.                                 doc = document;//document.defaultView.top;
  115.                                 for(var i = 0; i < 10; i++)//prevents infinite loop, just in case
  116.                                 {
  117.                                     if(!doc.SW_media_requests)
  118.                                     {
  119.                                         doc.SW_media_requests = [];
  120.                                     }
  121.                                     doc.SW_media_requests.push([uri, referrer, file_ext, filename]);
  122.                                     if(!doc.defaultView || doc == doc.defaultView.top)
  123.                                     {
  124.                                         break;
  125.                                     }
  126.                                     else
  127.                                     {
  128.                                         doc = doc.defaultView.parent.document;
  129.                                     }
  130.                                 }
  131.                                 
  132.                                 LOG("uri: " + uri + " ref: " + referrer + " fileext: " + file_ext + " filename: " + filename);
  133.                                 /*
  134.                                 var browserWindow = getDOMWindow(document.defaultView.top);
  135.                                 if(browserWindow.wrappedJSObject)
  136.                                 {
  137.                                     browserWindow = browserWindow.wrappedJSObject;
  138.                                 }
  139.                                 LOG(browserWindow.SmarterWiki);
  140.                                 */
  141.                             }
  142.                         }
  143.                         catch(e)
  144.                         {
  145.                             LOG("ERROR: " + e);
  146.                         }
  147.                     }
  148.                 }
  149.                 catch(e) { }
  150.             }
  151.             
  152.             if(aTopic == "app-startup") 
  153.             {
  154.                 var observerService = Components.classes["@mozilla.org/observer-service;1"]
  155.                              .getService(Components.interfaces.nsIObserverService);
  156.                 observerService.addObserver(this, "http-on-examine-response", false);
  157.                 observerService.addObserver(this, "http-on-examine-cached-response", false);
  158.                 LOG("registered everything!");
  159.                 return;
  160.             }            
  161.         }
  162.         catch(e)
  163.         {
  164.             LOG("ERROR: " + e);
  165.         }
  166.     },    
  167.     
  168.     QueryInterface: function (iid) 
  169.     {
  170.         if(iid.equals(Components.interfaces.nsIObserver) ||
  171.                 iid.equals(Components.interfaces.nsISupports))
  172.         {
  173.             return this;
  174.         }
  175.         Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  176.         return null;
  177.     }
  178. };
  179.  
  180. var _module = {
  181.     registerSelf: function (compMgr, fileSpec, location, type) 
  182.     {
  183.         var compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  184.         compMgr.registerFactoryLocation(this._classID,
  185.                                         this._classDescription,
  186.                                         this._contractID,
  187.                                         fileSpec,
  188.                                         location,
  189.                                         type);
  190.  
  191.         var categoryManager = Components.classes["@mozilla.org/categorymanager;1"]
  192.                                 .getService(Components.interfaces.nsICategoryManager);
  193.         categoryManager.addCategoryEntry("app-startup", this._classDescription, this._contractID, true, true);
  194.     },
  195.  
  196.     _classID: Components.ID("{67d054a6-7173-47e4-bf4a-dcd77b88fa59}"),
  197.     _classDescription: "SmarterFox HTTP Observer Javascript XPCOM Component",
  198.     _contractID: "@smarterwiki/http-observer;1",
  199.     _factory: {
  200.         QueryInterface: function(aIID) 
  201.         {
  202.             if (!aIID.equals(Components.interfaces.nsISupports) &&
  203.                 !aIID.equals(Components.interfaces.nsIFactory))
  204.             {
  205.                 throw Components.results.NS_ERROR_NO_INTERFACE;
  206.             }
  207.             return this;
  208.         },
  209.  
  210.         createInstance: function(outer, iid)
  211.         {
  212.             return new SmarterWikiHTTPObserver();
  213.         }
  214.     },
  215.  
  216.     getClassObject: function (compMgr, cid, iid) 
  217.     {
  218.         return this._factory;
  219.     },
  220.  
  221.     canUnload: function(compMgr) 
  222.     {
  223.         return true;
  224.     }
  225. };
  226.  
  227. function NSGetModule(compMgr, fileSpec) 
  228. {
  229.     return _module;
  230. }